package com.example.rule.expression;

import com.example.rule.db.entity.PymtRule;
import com.example.rule.db.entity.PymtRuleOperator;
import com.example.rule.core.Representation;
import com.example.rule.util.CacheUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author Jason - Chusifeng
 * @version 1.0.0
 * @className OneTimeExpression.java
 * @packageName com.example.rule.expression
 * @createTime 2022年10月12日 09:24:15
 * @description 创建当次转账金额大小的表达式
 */
public class OneTimeRepresentation implements Representation {

    private static final String EXPRESSION_FIELD ="money";

    @Override
    public  String[] createExpressions(PymtRule rule) {
        List<PymtRuleOperator> operatorList = CacheUtil.getRuleOperators();
        StringBuilder sb = new StringBuilder();
        sb.append(EXPRESSION_FIELD);
        sb.append(getOpe(rule.getOperator1(),operatorList)).append(rule.getValue1());
        sb.append(",");
        //operator2
        String operator = rule.getOperator2();
        if (StringUtils.isNotBlank(operator) ) {
            sb.append(EXPRESSION_FIELD);
            sb.append(getOpe(operator,operatorList)).append(rule.getValue2());
        }
        sb.append(",");
        //operator3
        operator = rule.getOperator3();
        if (StringUtils.isNotBlank(operator) ) {
            sb.append(EXPRESSION_FIELD);
            sb.append(getOpe(operator,operatorList)).append(rule.getValue3());
        }
        return sb.toString().split(",");
    }
    private static String getOpe(String name ,List<PymtRuleOperator>  operatorList ){
        PymtRuleOperator ruleOperator = operatorList.stream().filter( op -> op.getPkPymtRuleOperator().equals(Integer.valueOf(name))).findFirst().orElseThrow(() ->new RuntimeException("don't get operator"));
        return ruleOperator.getValue();
    }
}
